package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"web_app/logic"
	"web_app/models"
	"web_app/tools/response"
	va "web_app/tools/validator"
)

var loginNum int

func SignUpHandler(ctx *gin.Context) {
	loginNum++
	fmt.Println(loginNum)
	var uq models.UserRequest
	if err := ctx.ShouldBindJSON(&uq); err != nil {
		zap.L().Error("参数解析", zap.Error(err))
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.RError(ctx, response.CodeInvalidParam, err.Error())
			return
		}
		response.RError(ctx, response.CodeInvalidParam, va.GetError(errs))
		return
	}
	//2.业务处理
	err := logic.SignUp(&uq)
	//3.返回响应e
	if err != nil {
		response.RError(ctx, response.CodeInvalidParam, err.Error())
		return
	}
	response.RSuccess(ctx, gin.H{"msg": "success"})
	return

}

func UserListHandler(ctx *gin.Context) {
	u, err := logic.UserList(ctx)
	if err != nil {
		zap.L().Error("查询失败 UserListHandler:", zap.Error(err))
	}
	response.RSuccess(ctx, u)
	return
}

func UserTagHandler(ctx *gin.Context) {
	u, err := logic.UserTags(ctx)
	if err != nil {
		zap.L().Error("查询失败 UserListHandler:", zap.Error(err))
	}
	response.RSuccess(ctx, u)
	return
}
